const cloud = require('wx-server-sdk');
cloud.init();
const COS = require('cos-nodejs-sdk-v5');
const cos = new COS({
  SecretId: 'AKID4xd5MVPGyTlqlSwDJDvPp36xd7UggaGm', // 替换为你的腾讯云 SecretId
  SecretKey: 'NbcR2SebLtJn8MXbXL1ZL5nV3NVQ7yfM' // 替换为你的腾讯云 SecretKey
});

exports.main = async (event, context) => {
  const { filePath, cloudPath } = event;
  const bucket = 'YOUR_BUCKET_NAME'; // 替换为你的 COS 存储桶名称
  const region = 'YOUR_REGION'; // 替换为你的 COS 存储桶所在区域

  try {
    // 上传文件到 COS
    await cos.putObject({
      Bucket: bucket,
      Region: region,
      Key: cloudPath,
      Body: filePath,
      ACL: 'public-read' // 设置为公有读权限
    });

    // 生成永久链接
    const permanentUrl = `https://${bucket}.cos.${region}.myqcloud.com/${cloudPath}`;
    return {
      code: 0,
      message: '上传成功',
      data: {
        permanentUrl
      }
    };
  } catch (err) {
    console.error('上传失败：', err);
    return {
      code: -1,
      message: '上传失败'
    };
  }
};